package ttu.eclass.util;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

public abstract class Connector {
	private String hostIP = "";
	private int port = 0;
	private Socket hostSocket = null;
	private ObjectInputStream objectInputStream = null;
	private ObjectOutputStream objectOutputStream = null;

	public Connector(String hostIP, int port) {
		this.hostIP = hostIP;
		this.port = port;
	}

	public Object getObject() throws IOException, ClassNotFoundException{
		return objectInputStream.readObject();
	}

	public void sendObject(Object obj) throws IOException{
		objectOutputStream.writeObject(obj);
	}

	public void connect() throws UnknownHostException, IOException {
		this.hostSocket = new Socket(hostIP, port);
		objectInputStream = new ObjectInputStream(hostSocket.getInputStream());
		objectOutputStream = new ObjectOutputStream(hostSocket.getOutputStream());
	}

	public void disconnect() throws IOException {
		objectInputStream.close();
		objectOutputStream.close();
		this.hostSocket.close();
	}

}
